Method (computer programming)
part 8/14 · 22.4 KB total
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
abstract class Shape {
abstract int area(int h, int w); // abstract method signature
}
The following subclass extends the main class:
public class Rectangle extends Shape {
@Override
int area(int h, int w) {
return h * w;
}
}
Reabstraction
If a subclass provides an implementation for an abstract method, another subclass can make it abstract again. This is called reabstraction.
In practice, this is rarely used.
Example
In C#, a virtual method can be overridden with an abstract method. (This also applies to Java, where all non-private methods are virtual.)
class IA
{
public virtual void M() { }
}
abstract class IB : IA
{
public override abstract void M(); // allowed
}
Interfaces' default methods can also be reabstracted, requiring subclasses to implement them. (This also applies to Java.)
interface IA
{
void M() { }
}
interface IB : IA
{
abstract void IA.M();
}
class C : IB { } // error: class 'C' does not implement 'IA.M'.
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────